/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package codemanager.widget;

import codemanager.database.dao.impl.ProjectDAOImpl;
import codemanager.database.pojo.Project;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

/**
 *
 * @author undefined
 */
public class CMNewProDialog extends javax.swing.JDialog {

    /**
     * Creates new form CMNewProDialog
     */
    public CMNewProDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        //使对话框居中显示
        Dimension winSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((int)(winSize.getWidth()/2-250), (int)(winSize.getHeight()/2-100), 500, 200);
        
    }

    public CMNewProDialog() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        scanButton = new javax.swing.JButton();
        certainButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("新建项目");
        setAlwaysOnTop(true);
        setBackground(java.awt.Color.lightGray);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setPreferredSize(new java.awt.Dimension(500, 100));
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel1.setBorder(BorderFactory.createTitledBorder("新建项目"));

        jLabel1.setText("项目路径：");

        jTextField1.setToolTipText("");

        scanButton.setText("浏览...");
        scanButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                scanButtonMouseClicked(evt);
            }
        });
        scanButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                scanButtonActionPerformed(evt);
            }
        });

        certainButton.setText("确定");
        certainButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                certainButtonMouseClicked(evt);
            }
        });

        cancelButton.setText("取消");
        cancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancelButtonMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(scanButton)
                .addContainerGap(59, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(certainButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cancelButton)
                .addGap(4, 4, 4))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(scanButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(certainButton)
                    .addComponent(cancelButton)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(24, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void scanButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_scanButtonActionPerformed
        
    }//GEN-LAST:event_scanButtonActionPerformed

    private void scanButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_scanButtonMouseClicked
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fileChooser.setVerifyInputWhenFocusTarget(true);
       // fileChooser.setAcceptAllFileFilterUsed(false);
      /*  fileChooser.setFileFilter(new FileFilter(){
           @Override
           public boolean accept(File f) {
               if(f.isDirectory()){
                   return true;
               }else{
                   return false;
               }
           }
           @Override
           public String getDescription() {
               return "文件夹";
           }
        });*/
        int rev = fileChooser.showDialog(this,"选择");
        if(rev == JFileChooser.APPROVE_OPTION){
        jTextField1.setText(fileChooser.getSelectedFile().getAbsolutePath());
        }
    }//GEN-LAST:event_scanButtonMouseClicked

    private void certainButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_certainButtonMouseClicked
        //get file path
        String proPath = jTextField1.getText();
        Date today = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if("".equals(proPath)||null==proPath){
            return;
        }
        //close the proDialog
        getOwner().dispose();
        //insert data of new project into DB
        ProjectDAOImpl projectDAOImpl = new ProjectDAOImpl();
        Project project = new Project();
        //project name
        project.setProName(proPath.substring(proPath.lastIndexOf("\\")+1, proPath.length()));
        //project's create date
        project.setCreateDate(dateFormat.format(today));
        //project's url
        project.setProURL(proPath);
        //presistence
        projectDAOImpl.insertProject(project);
        
        ((JFrame)getOwner()).repaint();
    }//GEN-LAST:event_certainButtonMouseClicked

    private void cancelButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancelButtonMouseClicked
        dispose();
    }//GEN-LAST:event_cancelButtonMouseClicked

   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton certainButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JButton scanButton;
    // End of variables declaration//GEN-END:variables
}
